package com.vercon.moensmart.popupwindow

import android.content.Context
import android.graphics.Color
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.vercon.moensmart.R
import com.vercon.moensmart.event.LoginEvent
import org.greenrobot.eventbus.EventBus
import razerdp.basepopup.BasePopupWindow


class UserEditPopupWindow : BasePopupWindow {

    val TAG = javaClass.simpleName

    private var mListener: UserEdtPopupWindowListener? = null

    lateinit var user_exit: TextView

    lateinit var fragmentManager: FragmentManager



    constructor(context: Context, fragmentManager: FragmentManager) : this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, fragmentManager)

    constructor(context: Context, w: Int, h: Int, fragmentManager: FragmentManager) : super(context, w, h) {
        this.fragmentManager = fragmentManager
        bindView()
    }

    override fun onCreateContentView(): View = createPopupById(R.layout.popupwindow_smarthome_user_edt)

    override fun dismiss() {
        super.dismiss()
    }


    override fun showPopupWindow() {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        wm.defaultDisplay.getMetrics(dm)
        val width = dm.widthPixels
        val density = dm.density
        val screenWidth = width.toInt()
        super.showPopupWindow(context.resources.getDimension(com.vercon.common.R.dimen.dp_130).toInt(),
            context.resources.getDimension(com.vercon.common.R.dimen.dp_140).toInt())
    }



    fun bindView() {
        //点击外面关闭
        //isDismissWhenTouchOutside = true
        // 不拦截点击事件
        //isInterceptTouchEvent = false
        setAllowInterceptTouchEvent(false)
        setOutSideDismiss(true)
        setBackgroundColor(Color.TRANSPARENT)

        user_exit = findViewById(R.id.user_exit)


        user_exit.setOnClickListener {
            mListener?.onExit()
            dismiss()
        }
    }

    //用户退出逻辑
    fun exit() {
        EventBus.getDefault().post(LoginEvent())
    }

    fun setUserEdtPopupWindowListener(listener : UserEdtPopupWindowListener){
        mListener = listener
    }

    interface UserEdtPopupWindowListener{

        fun onGotoLogin()

        fun onExit()

        fun onCancle()

    }
}